
from feat.plotting import plot_face
import numpy as np
import matplotlib.pyplot as plt
from feat import Detector
import time
import json

class FaceAna():
    def __init__(self):
        self.detector = None
        try:
            self.detector = Detector(
                face_model="retinaface",
                landmark_model="mobilefacenet",
                au_model='xgb',
                emotion_model="resmasknet",
                facepose_model="img2pose",
                n_jobs=4,
            )
        except Exception as e:
            print("微表情分析开启失败:::>>::",e)

    def anaAU(self,url):
        # s = time.time()
        # print("============")
        # # neutral = np.zeros(20)
        single_face_prediction = self.detector.detect_image(url)
        e = time.time()
        # print("===========",e-s)
        print(single_face_prediction.to_json(orient="records"))
        anaRS = json.loads(single_face_prediction.to_json(orient="records"))[0]
        # aKlist = anaRS.keys()
        anaRS = {k: v for k, v in anaRS.items() if k in  ["anger", "disgust", "fear", "happiness", "sadness", "surprise", "neutral","AU01", "AU02", "AU04", "AU05", "AU06", "AU07", "AU09", "AU10", "AU11", "AU12", "AU14", "AU15", "AU17", "AU20", "AU23", "AU24", "AU25", "AU26", "AU28", "AU43"]}
        # figs = single_face_prediction.plot_detections(poses=False)
        print(anaRS)
        return anaRS